package com.ruoyi.syf.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.syf.domain.RiskEvaluationAccidentDetail;
import com.ruoyi.syf.handler.deleteChain.DeleteService;
import com.ruoyi.syf.mapper.RiskEvaluationAccidentDetailMapper;
import com.ruoyi.syf.service.IRiskEvaluationAccidentDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 风险判定-事故明细Service业务层处理
 *
 * @author yr
 * @date 2024-07-04
 */
@Service
public class RiskEvaluationAccidentDetailServiceImpl extends ServiceImpl<RiskEvaluationAccidentDetailMapper,RiskEvaluationAccidentDetail> implements IRiskEvaluationAccidentDetailService {
    @Autowired
    private RiskEvaluationAccidentDetailMapper riskEvaluationAccidentDetailMapper;
    @Lazy
    @Autowired
    private DeleteService deleteService;

    /**
     * 查询风险判定-事故明细
     *
     * @param id 风险判定-事故明细主键
     * @return 风险判定-事故明细
     */
    @Override
    public RiskEvaluationAccidentDetail selectRiskEvaluationAccidentDetailById(Long id) {
        return riskEvaluationAccidentDetailMapper.selectById(id);
    }

    @Override
    public boolean saveList(Long evaluationId, List<Long> accidentTypeIds) {
        List<RiskEvaluationAccidentDetail> list = new ArrayList<>();
        accidentTypeIds.forEach(id -> {
            RiskEvaluationAccidentDetail detail = new RiskEvaluationAccidentDetail();
            detail.setEvaluationId(evaluationId);
            detail.setAccidentTypeId(id);
            list.add(detail);
        });
        return saveBatch(list);
    }

    @Override
    public List<RiskEvaluationAccidentDetail> getListByEvaluationId(Long evaluationId) {
        LambdaQueryWrapper<RiskEvaluationAccidentDetail> lqw = new LambdaQueryWrapper<RiskEvaluationAccidentDetail>()
                .eq(RiskEvaluationAccidentDetail::getEvaluationId,evaluationId)
                .eq(RiskEvaluationAccidentDetail::getDelFlag,"0");
        return riskEvaluationAccidentDetailMapper.selectList(lqw);
    }

    @Override
    public boolean updateByEvaluationId(Long evaluationId,List<Long> accidentList) {
        List<RiskEvaluationAccidentDetail> list = getListByEvaluationId(evaluationId);
        deleteService.deleteDetail(list.stream().map(RiskEvaluationAccidentDetail::getId).collect(Collectors.toList()));
        //删除后保存
        saveList(evaluationId,accidentList);
        return true;
    }

    @Override
    public boolean deleteByEvaluationId(List<Long> evaluationIds) {
        LambdaQueryWrapper<RiskEvaluationAccidentDetail> lqw = new LambdaQueryWrapper<RiskEvaluationAccidentDetail>()
                .in(RiskEvaluationAccidentDetail::getEvaluationId,evaluationIds)
                .eq(RiskEvaluationAccidentDetail::getDelFlag,"0");
        List<RiskEvaluationAccidentDetail> list = riskEvaluationAccidentDetailMapper.selectList(lqw);
        return removeBatchByIds(list);
    }
}
